// JavaScript Document
//index首页发表js
$(function() {
    function judge(text) {
		var sl = $('.sl').attr('value');
		if (!sl) {
			$('.modal-title').html(text);
			$('#myModal').modal('toggle');
			return false;
		} else {
			return true;
		}
	}

	$('.publish').click(function() {
		var title = $('#input-head').val();
		var content = $('#input-body').html();
		var articleType
		var invite = $('#appImg').attr('value');
		var display = $('#articlediv').css('display');
		if (display == 'none') {
			articleType = 2;
		} else if (display == 'block') {
			articleType = 1;
		}
		if (content) {
			articleAja(url, title, content, articleType, '', '', '', invite);
		} else {
			Aprompt("写点东西呗~");
		}
	});
	function articleAja(url, title, content, articleType, articleId,
			goodIndexOf, Athis, invite) {
			$(".publish").fadeOut("slow");
			var opts = {
					lines: 13, // The number of lines to draw
					length: 11, // The length of each line
					width: 5, // The line thickness
					radius: 17, // The radius of the inner circle
					corners: 1, // Corner roundness (0..1)
					rotate: 0, // The rotation offset
					color: '#FFF', // #rgb or #rrggbb
					speed: 1, // Rounds per second
					trail: 60, // Afterglow percentage
					shadow: false, // Whether to render a shadow
					hwaccel: false, // Whether to use hardware acceleration
					className: 'spinner', // The CSS class to assign to the spinner
					zIndex: 2e9, // The z-index (defaults to 2000000000)
					top: 'auto', // Top position relative to parent in px
					left: 'auto' // Left position relative to parent in px
						};
			var target = document.createElement("div");
			document.body.appendChild(target);
			var spinner = new Spinner(opts).spin(target);
			var overlay = iosOverlay({
				text: "加载中..",
				spinner: spinner,
			});
		$.ajax({
					url : url,
					type : 'POST',
					data : {
						"title" : title,
						"content" : content,
						"articleType" : articleType,
						"articleId" : articleId,
						"invite" : invite
					},
					success : function(data) {
						if (data == "false") {	
							overlay.update({
								text: "网络好像有点卡",
								icon: urlImages + "iosOverlay/cross.png"
							});
							setTimeout(function() {
							overlay.hide();
							}, 3e3);
							$(".publish").fadeIn("slow");
						} else {
							// var a=123;
							
							var Jarticle;
							try{
								Jarticle = JSON.parse(data);
							}catch(e){
								overlay.update({
								text: "网络好像有点卡",
								icon: urlImages + "iosOverlay/cross.png"
								});
								setTimeout(function() {
								overlay.hide();
								}, 3e3);
								$(".publish").fadeIn("slow");
							}
							var addAct = '<div class="card panel panel-default">';
							addAct += '<div class="panel-body"><div class="card-head">';
							addAct += '<div class="card-head-left"><a href="'
									+ articleUrl
									+ '/hispage?hispage='
									+ Jarticle["user_id"]
									+ '"><img src='
									+ Jarticle["user_img"]
									+ '      width="45" class="img-circle" /></a></div>';
							addAct += '<div class="card-head-center"><a href="'
									+ articleUrl + '/hispage?hispage='
									+ Jarticle["user_id"] + '">'
									+ Jarticle["user_name"] + '</a><small>@'
									+ Jarticle["article_time"]
									+ '</small></div>';
							if (articleType == '2' || articleType == '6') {
								if (Jarticle["is_order"] == '1') {
									addAct += '<div class="card-head-right">' +									
										'<span class="fa fa-eye"  title="浏览量"> 0</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
										'<span class="fa fa-rss marking"  title="订阅数"> '+ Jarticle['article_order'] + '</span>' + 								
									'</div>';
											
								} else {
									addAct += '<div class="card-head-right"> ' +  
										'<span class="fa fa-eye"  title="浏览量"> 0</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
										 '<span class="order cursor " value="'
											+ Jarticle['article_id']
											+ '">' + 
											'<span class="fa fa-rss"  title="订阅数"> ' + Jarticle["article_order"] + '</span>'
											+ '</div>';
								}
							} else {
								addAct += '<div class="card-head-right">' + 
										'<span class="fa fa-eye"  title="浏览量"> 0</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
										'<i class="fa fa-thumbs-up good  cursor">'
										+ Jarticle["article_zan"]
										+ '</i>&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-thumbs-down  cursor">'
										+ Jarticle["article_dao"]
										+ '</i></div>';

							}

							addAct += '<div class="clearfix"></div>';
							addAct += '</div>';
							addAct += '<div class="card-body">';
							switch (articleType) {
							case 1:
								addAct += ' <div class="title discuss"><span class="titleCont"><a href="'
										+ articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							case 2:
								addAct += ' <div class="title discuss">发起 <span>讨论</span><span class="titleCont"><a href="'
										+ articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							case 3:
								addAct += ' <div class="title discuss">参与 <span>讨论</span><span class="titleCont"><a href="'
										+ articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							case 4:
								addAct += ' <div class="title discuss">赞同 <span class="rss">文章</span><span class="titleCont"><a href="'
										+ articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							case 5:
								addAct += ' <div class="title discuss">批阅 <span class="rss">文章</span><span class="titleCont"><a href="'
										+ articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/article?aid='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							case 6:
								addAct += ' <div class="title discuss">订阅 <span>讨论</span><span class="titleCont"><a href="'
										+ articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"]
										+ '">'
										+ Jarticle["article_title"]
										+ '</a></span></div>';
								addAct += '<a href="' + articleUrl
										+ '/discuss?aId='
										+ Jarticle["article_id"] + '">'
										+ Jarticle["article_content"]
										+ '</a></div>';
								break;
							}

							addAct += '<div class=""></div>';
							addAct += '</div>';
							addAct += '</div>';

							$(addAct).prependTo('#appCont');

							$('#exampleInputPassword1').val("");
							overlay.update({
								icon: urlImages + "iosOverlay/check.png",
								text: "发起成功",
							});
							slideUpEditPanel();
							setTimeout(function() {
								overlay.hide();
								$(".publish").fadeIn("slow");
								$("#imge").slideUp("slow");
								$("#input-footer").slideDown("slow");
								$("#btn-toolbar").slideDown("slow");
							}, 3e3);
							
							if (Jarticle["article_type"] == '3'|| Jarticle["article_type"] == '6') {
								Athis.html(Jarticle['article_order']);
								Athis.addClass('marking').removeClass('order');
							} else {
								if (goodIndexOf != -1) {
									Athis.addClass('fa-thumbs-up').removeClass(
											'fa-thumbs-o-up');
									Athis.html(Jarticle['article_zan']);

								} else {
									Athis.addClass('fa-thumbs-down')
											.removeClass('fa-thumbs-o-down');
								}
							}
						}
					}

				});
	}
	function Aprompt(cont) {
		$('#input-body').css('background', '#abcdef').html(cont);
		$('#input-head').val('');

		setTimeout(function() {
			$('#input-body').css('background', '').html("");

		}, 500);
	}

	// 点赞模块
	$(document).on('click','.card-head-right i',function() {
				var $indexOf = $(this).attr('class');
				var zanIndexof = $indexOf.indexOf('fa-thumbs-up');
				var daoIndexof = $indexOf.indexOf('fa-thumbs-down');
				var goodIndexOf = $indexOf.indexOf('good');
				if (goodIndexOf != -1) {
					var text = '为了避免点赞狂魔，支持我要先登录噢';
				} else {
					var text = '不认同我？期待和你切磋切磋';
				}
				if (!judge(text))
					return false;
				if (zanIndexof == 3 || daoIndexof == 3) {
					$(this).removeClass('cursor');
				} else {
					var articleId = $(this).attr('value');

					var articleType = 4;
					var content = $(this).parent().parent().siblings('.card-body').find('.articleCont > a').html();
					var title = $(this).parent().parent().siblings('.card-body').find('.titleCont > a').html();
					if (goodIndexOf != -1) {
						articleAja(url, title, content, articleType, articleId,
								goodIndexOf, $(this));// 调用ajax函数

					} else {// 这是倒的模块
						daoThis = $(this);
						$.ajax({
							url : urlDown,
							type : 'POST',
							data : {
								"articleId" : articleId
							},
							success : function(data) {
								var JionArticle = JSON.parse(data);
								daoThis.html(JionArticle['article_dao']);
								daoThis.addClass('fa-thumbs-down').removeClass(
										'fa-thumbs-o-down');
							}
						});

					}

				}
			});
	// 关注模块
	$('.attention').click(function() {
		var text = "想关注我？让我看看你是何方神圣";
		if (!judge(text))
			return false;
		var userId = $(this).attr('value');
		var userThis = $(this);
		$.ajax({
			url : fileUrl,
			type : 'POST',
			data : {
				"userId" : userId
			},
			success : function(data) {
				if (data == 'true') {

					$('.attention').html("√ 已关注").removeClass('attention');
				}
			}
		});
	});
	// 桃心点赞模块
	$('.heart').click(function() {
		var text = '你欣赏我？快让我知道你是谁';
		if (!judge(text))
			return false;
		var userId = $(this).attr('value');
		var userThis = $(this);
		$.ajax({
			url : heartUrl,
			type : 'POST',
			data : {
				"userId" : userId
			},
			success : function(data) {
				$('.heart').html(data).removeClass('heart');
			}
		});
	});


	// 这是消息阅读模块
	$(document).on('click','.messRead',function() {
		var type = $(this).attr('value');
		$.ajax({
			url : urlMessRead,
			dataType : "JSON",
			type : 'POST',
			data : {
				'type' : type
			},
			success : function(data) {
			}
		});
	});
	
	
	// 加载更多
	$('.fildLoad').click(function() {
		Athis = $(this);
		if($(this).html() != '努力加载中...')
		{
			$(this).html('努力加载中...');
			var max_id = $(this).attr("value");
			$.ajax({
						url : location.href,
						dataType : "JSON",
						type : 'POST',
						data : {
							'max_id' : max_id,
							'tab' : $('#hallTab .active a').attr('href'),
							'tags' : function()
							{
								var result = new Array();
								$('#fav span span').each(function(index, element) {
									result.push($(this).attr('value'));    
                                });							
								return result;
							}
						},
						success : function(data) {
							if ( data == null || data.length == 0) {
								Athis.html('已经到底了，约朋友出去聊聊吧~');
							} else {
								doAppend(Athis,data,0,max_id);
							}
						},
						error : function(XMLResponse){
							Athis.html('貌似网速没跟上我的节奏╭(╯^╰)╮');
						}

				});
		}
	});
	
	function doAppend(Athis,data,i,page)
	{	
									var left = Athis.parent().find('.leftCont');
									var right = Athis.parent().find('.rightCont');
									var panel = left;
									if(left.height() > right.height())
										panel = right;
									var html = '<div id="new_' + page + '_' + i + '" class="card panel panel-default" style="display:none">' +
												'<div class="panel-body">' +
													'<div class="card-head">' +
														'<div class="card-head-left">' +
															'<a href="' + articleUrl + '/hispage?hispage=' + data[i].user_id + '">' +
																'<img src="' + data[i].user_img + '" width="45" class="img-circle">' +
															'</a>' +
														'</div>' +
														'<div class="card-head-center">' +
															'<a href="' + articleUrl + '/hispage?hispage=' + data[i].user_id + '">' +
																data[i].user_name +
															'</a>' +
															'<small>@' + 
																data[i].article_time + 
															'</small>' +
														'</div>';
														
							if(data[i].article_type=='2' || data[i].article_type=='6')
							{
								if(data[i].is_order==1)
								{
									html+=				'<div class="card-head-right">' +
																'<span class="fa fa-eye"  title="浏览量"> ' + data[i].article_num + ' </span>&nbsp;&nbsp;&nbsp;&nbsp;' +
																'<span class="fa fa-rss marking"  title="订阅数"> '+ data[i].article_order + '</span>' + 
														'</div>';
														
								}
								else
								{
									html+=				'<div class="card-head-right">' +
															'<span class="fa fa-eye"  title="浏览量"> ' + data[i].article_num + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
															'<span class="order cursor" value=' + data[i].article_id + '>'+
															'<span class="fa fa-rss"  title="订阅数"> ' + data[i].article_order + '</span> ' +
															'</span>' + 
														'</div>';
								}
							}
							else
							{
								if(data[i].article_zan_id == 1 || data[i].article_dao_id == 1)
								{
									html+=				'<div class="card-head-right">' + 
															'<span class="fa fa-eye"  title="浏览量"> ' + data[i].article_num + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
															'<i class="fa fa-thumbs-up good   cursor">' + data[i].article_zan + 
															'</i>' + 
															'&nbsp;&nbsp;&nbsp;&nbsp;' +
															'<i class="fa fa-thumbs-down   cursor">' + data[i].article_dao +
															'</i>' +
														'</div>';
								}
								else
								{
										html+=				'<div class="card-head-right">' +
															'<span class="fa fa-eye"  title="浏览量"> ' + data[i].article_num + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
															'<i value="'+data[i].article_id+'" class="fa fa-thumbs-o-up good   cursor p_' + page + '_' + i + '">'+
																data[i].article_zan+
															'</i>' + 
															'&nbsp;&nbsp;&nbsp;&nbsp;' +
															'<i value="'+data[i].article_id + '" class="fa fa-thumbs-o-down   cursor p_' + page + '_' + i + '">'+
																data[i].article_dao +
															'</i>' + 
														'</div>';	
								}
							}
										html+=			'<div class="clearfix">' +
														'</div>' +
													'</div>';
																
																
																
										html+=		'<div class="card-body">';
  											                        
						switch(data[i].article_type){
							case '1':html+=				'<div class="title discuss">'+
															'<span class="titleCont">'+
																'<a href="'+articleUrl+'/article?aid='+data[i].article_id+'">'+ 
																	data[i].article_title+
																'</a>' +
															'</span>' +
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+articleUrl+'/article?aid='+data[i].article_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							case '2':html+=				'<div class="title discuss">' +
															'发起' +
															'<span>' +
																'讨论' +
															'</span>' +
															'<span class="titleCont">' +
																'<a href="'+urlDiscuss+'?aId='+data[i].article_id+'">'+
																	data[i].article_title +
																'</a>' +
															'</span>' +
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+urlDiscuss+'?aId='+data[i].article_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							case '3':html+=				'<div class="title discuss">' +
															'参与' +
															'<span>' +
																'讨论' +
															'</span>' +
															'<span class="titleCont">' +
																'<a href="'+urlDiscuss+'?aId='+data[i].article_father_id+'">'+
																	data[i].article_title +
																'</a>' +
															'</span>' +
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+urlDiscuss+'?aId='+data[i].article_father_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							case '4':html+=				'<div class="title discuss">' +
															'赞同' +
															'<span class="rss">' +
																'文章' +
															'</span>' +
															'<span class="titleCont">' +
																'<a href="'+articleUrl+'/article?aid='+data[i].article_father_id+'">'+
																	data[i].article_title +
																'</a>' +
															'</span>' +
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+articleUrl+'/article?aid='+data[i].article_father_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							case '5':html+=				'<div class="title discuss">' +
															'批阅' +
															'<span class="rss">' +
																'文章' +
															'</span>' +
															'<span class="titleCont">' +
																'<a href="'+articleUrl+'/article?aid='+data[i].article_father_id+'">' +
																	data[i].article_title +
																'</a>' +
															'</span>' +
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+articleUrl+'/article?aid='+data[i].article_father_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							case '6':html+=				'<div class="title discuss">' +
															'订阅' +
															'<span>' +
																'讨论' +
															'</span>' +
															'<span class="titleCont">' +
																'<a href="'+urlDiscuss+'?aId='+data[i].article_father_id+'">' +
																	data[i].article_title +
																'</a>' +
															'</span>' + 
														'</div>' +
														'<div class="articleCont">' +
															'<a href="'+urlDiscuss+'?aId='+data[i].article_father_id+'">' +
																data[i].article_content + 
															'</a>' +
														'</div>' +
													'</div>';
							break;
							}
							html+=					'<div class="">' +
													'</div>' +
                      							'</div>' +
                      						'</div>';
							
							
							if(!panel.is(':visible'))
							{
								
								Athis.html('点击加载更多');
								if(i > 0)
									Athis.attr('value', data[i - 1].article_id);
							}
							else
							{
								
								panel.append(html);
								$("#new_" + page + "_" + i).fadeIn('slow',function(){
									if(i + 1 < data.length)
										doAppend(Athis,data,i + 1,page);
									else
									{
										Athis.html('点击加载更多');
										if(data.length > 0)
											Athis.attr('value', data[data.length - 1].article_id);
									}
								});
							}
	}
	$(window).scroll(function(e) {
			var scrollTop = $(this).scrollTop();               //滚动条距离顶部的高度
			var scrollHeight = $(document).height();           //当前页面的总高度
			var windowHeight = $(this).height();               //当前可视的页面高度
			if(scrollTop + windowHeight >= scrollHeight){        //距离顶部+当前高度 >=文档总高度 即代表滑动到底部
				$('.fildLoad').each(function(index, element) {
                   if($(this).is(':visible'))
				   		$(this).click(); 
                });
			}
		});
	// 这是订阅模块
	$(document).on('click','.order',function() {
		var text = '欲知后事如何，请先登录可好？';
		if (!judge(text))
			return false;
		var articleType = 6;
		var articleId = $(this).attr('value');
		var content = $(this).parent().parent().siblings('.card-body')
				.find('.articleCont > a').html();
		var title = $(this).parent().parent().siblings('.card-body')
				.find('.titleCont > a').html();
		$.ajax({
					url : urlOrder,
					type : 'POST',
					data : {
						"title" : title,
						"content" : content,
						"articleType" : articleType,
						"articleId" : articleId,
					}
		});
		var num = $(this).children('span').html().substr(1);
		$(this).children('span').html(' ' + (++num)).addClass('marking');
		});
});
